<template>
	<view class="header" :style="{ paddingTop: safeAreaInsets.top + 'px' }">
		<view class="back-button" @click="goBack">
			<uni-icons type="left" size="24" color="#fff"></uni-icons>
		</view>
		<view class="user-info">
			<view class="avatar-box">
				<view class="gradient-circle large">
					<image class="avatar-img" :src="user.avatar" @error="imageError">
					</image>
				</view>
			</view>
			<view class="flex-center">
				<view v-if="user && Object.keys(user).length > 0">
					<view class="name-row">
						<text class="username">{{ user.nickName }}</text>
					</view>
				</view>
			</view>
		</view>
		<view class="signature">{{ userAdditionalInfo.bio || '暂无个性签名' }}</view>
		<view class="sign">
			<view class="flex-center">
				<view class="gender-badge">
					<view class="gender-b" v-if="user.sex === '0'">男</view>
					<view class="gender-g" v-else>女</view>
				</view>
			</view>
		</view>
		<!-- 数据面板 -->
		<view class="data-panel">
			<view class="user-data">
				<view v-for="item in panelData" :key="item.label">
					<view class="user-number">{{ item.value }}</view>
					<view class="user-label">{{ item.label }}</view>
				</view>
			</view>
			<view class="follow-btns" v-if="!isCurrentUser">
				<view class="btn" :class="{ 'followed': isFollowed }" @click="handleFollow">
					{{ isFollowed ? '已关注' : '关注' }}
				</view>
			</view>
		</view>
	</view>
</template>

<script setup lang="ts">
	import { defineProps, defineEmits, computed } from 'vue'
	import { addFollow, delFollow, type UserFollow } from '@/api/follow/follow'
	import useUserStore from '@/store/modules/user'

	// 获取屏幕边界到安全区域距离
	const { safeAreaInsets } = uni.getWindowInfo()
	const userStore = useUserStore()

	const props = defineProps<{
		userInfo: Object,
		useradditionalinfo: Object,
		panelData: PanelDataItem[],
		isFollowed: boolean,
		followId?: number
	}>()

	// 将 user 和 userAdditionalInfo 定义为 computed 属性，以响应 props 的变化
	const user = computed(() => props.userInfo)
	const userAdditionalInfo = computed(() => props.useradditionalinfo)

	// 判断是否是当前用户自己
	const isCurrentUser = computed(() => {
		return userStore.user && userStore.user.userId === user.value?.userId
	})

	interface PanelDataItem {
		value: number | string
		label: string
	}

	// 事件定义
	const emit = defineEmits<{
		followChange: [isFollowed: boolean],
		imageError: []
	}>()

	const handleFollow = async (): Promise<void> => {
		try {
			// 检查用户是否已登录
			if (!userStore.user || !userStore.user.userId) {
				uni.showToast({
					title: '请先登录',
					icon: 'none'
				})
				return
			}

			// 不能关注自己
			if (isCurrentUser.value) {
				uni.showToast({
					title: '不能关注自己',
					icon: 'none'
				})
				return
			}

			const followData: UserFollow = {
				followedUserId: user.value.userId,
				fansId: userStore.user.userId
			}

			if (!props.isFollowed) {
				// 关注用户
				const res = await addFollow(followData)
				if (res.code === 200) {
					emit('followChange', true)
					uni.showToast({
						title: '关注成功'
					})
				} else {
					uni.showToast({
						title: res.msg || '关注失败',
						icon: 'none'
					})
				}
			} else {
				// 取消关注
				const res = await delFollow(followData)
				if (res.code === 200) {
					emit('followChange', false)
					uni.showToast({
						title: '已取消关注'
					})
				} else {
					uni.showToast({
						title: res.msg || '取消关注失败',
						icon: 'none'
					})
				}
			}
		} catch (error) {
			console.error('关注操作失败:', error)
			uni.showToast({
				title: '操作失败，请重试',
				icon: 'none'
			})
		}
	}

	const imageError = (): void => {
		emit('imageError')
		console.log('图片加载失败')
	}

	// 返回上一页的方法
	const goBack = () => {
		uni.navigateBack()
	}
</script>

<style lang="scss" scoped>
	.gradient {
		&-circle {
			border-radius: 50%;
			background: white;

			&.large {
				width: 130rpx;
				height: 130rpx;
				border: 4rpx solid #fff;
				box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.1);
			}
		}
	}

	.flex-center {
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.header {
		background: linear-gradient(180deg, #1C2D41 0%, #546366 100%);
		padding: 100rpx 30rpx 80rpx 30rpx;

		.back-button {
			// position: absolute;
			top: 20rpx;
			/* 根据实际布局调整 */
			left: 20rpx;
			/* 根据实际布局调整 */
			// z-index: 10;
		}

		.user-info {
			margin-top: 30rpx;
			display: flex;

			.avatar-box {
				position: relative;
				margin-right: 32rpx;

				.avatar-img {
					width: 130rpx;
					height: 130rpx;
					border-radius: 50%;
				}
			}
		}

		.name-row {
			display: flex;
			align-items: center;
			margin-bottom: 10rpx;

			.username {
				color: #EFFAFB;
				font-size: 30rpx;
				font-weight: 700;
				margin-right: 16rpx;
			}
		}

		.signature {
			color: #BCC1C4;
			font-size: 28rpx;
			padding-top: 34rpx;
		}

		.sign {
			display: flex;
			padding-top: 20rpx;

			.gender-badge {
				display: flex;
				text-align: center;
				align-items: center;
				justify-content: center;
				background-color: #707F84;
				width: 40rpx;
				height: 40rpx;
				padding: 2rpx 10rpx;
				font-size: 20rpx;
				border-radius: 40rpx;
				text-align: center;
				line-height: 36rpx;

				.gender-b {
					color: #0000ff;
				}

				.gender-g {
					color: #FF8597;
				}
			}
		}
	}

	.data-panel {
		display: flex;
		justify-content: space-between;
		font-size: 26rpx;
		padding: 50rpx 0;
		color: #BCC1C4;

		.user-data {
			display: flex;
			justify-content: space-between;
			gap: 50rpx;

			.user-number {
				color: #EFFAFB;
				display: flex;
				justify-content: center;
				align-items: center;
			}

			.user-label {
				font-size: 22rpx;
				display: flex;
				justify-content: center;
				align-items: center;
			}
		}

		.follow-btns {
			display: flex;
			gap: 16rpx;

			.btn {
				padding: 0 32rpx;
				border-radius: 40rpx;
				color: #EFFAFB;
				border: 1rpx solid rgba(248, 251, 251, 0.296);
				background-color: #637376;
				display: flex;
				align-items: center;
				cursor: pointer;
				transition: all 0.3s ease;

				&.followed {
					background-color: #4CAF50;
					border-color: #4CAF50;
				}

				&:active {
					opacity: 0.8;
				}
			}
		}
	}
</style>